package cn.tx.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class SourceGenerator {
	
	public static void main(String[] args) throws Exception {
		generate("Menu");
		
		//genQuery("Menu");
	}
	
	public static void generate(String className) throws Exception{
		genQuery(className);
		genDao(className);
		genService(className);
		genAction(className);
		genDaoConfig(className);
		genServiceConfig(className);
		genControllerConfig(className);
		genStrutsConfig(className);
		
	}
	
	public static void genDao(String className) throws Exception{
		
		
		BufferedReader br = new BufferedReader(new FileReader("src/cn/tx/dao/DemoDao.txt"));
		BufferedWriter out = new BufferedWriter(new FileWriter("src/cn/tx/dao/"+className+"Dao.java"));
		copy(br, out, className);
		
		
		BufferedReader br1 = new BufferedReader(new FileReader("src/cn/tx/dao/impl/DemoDaoImpl.txt"));
		BufferedWriter out1 = new BufferedWriter(new FileWriter("src/cn/tx/dao/impl/"+className+"DaoImpl.java"));
		copy(br1, out1, className);
	}
	
	public static void genQuery(String className) throws Exception{
	
		
		BufferedReader br = new BufferedReader(new FileReader("src/cn/tx/query/DemoQuery.txt"));
		BufferedWriter out = new BufferedWriter(new FileWriter("src/cn/tx/query/"+className+"Query.java"));
		copy(br, out, className);
		
		
		
	}
	
	public static void genService(String className) throws Exception{
		
		
		BufferedReader br = new BufferedReader(new FileReader("src/cn/tx/service/DemoService.txt"));
		BufferedWriter out = new BufferedWriter(new FileWriter("src/cn/tx/service/"+className+"Service.java"));
		copy(br, out, className);
		
		
		BufferedReader br1 = new BufferedReader(new FileReader("src/cn/tx/service/impl/DemoServiceImpl.txt"));
		BufferedWriter out1 = new BufferedWriter(new FileWriter("src/cn/tx/service/impl/"+className+"ServiceImpl.java"));
		copy(br1, out1, className);
	}
	public static void genAction(String className) throws Exception{
		
		
		BufferedReader br = new BufferedReader(new FileReader("src/cn/tx/controller/DemoAction.txt"));
		BufferedWriter out = new BufferedWriter(new FileWriter("src/cn/tx/controller/"+className+"Action.java"));
		copy(br, out, className);
		
	
	}
	
	public static void copy(BufferedReader br, BufferedWriter out, String className) throws Exception{
		String lowerClassName = className.substring(0, 1).toLowerCase() + className.substring(1);
		String line = null;
		while((line = br.readLine()) != null){
			line = line.replace("Demo", className).replace("demo", lowerClassName);
			out.write(line);
			out.newLine();
		}
		out.close();
		br.close();
	}
	
	public static void genDaoConfig(String className) throws Exception{
		String lowerClassName = className.substring(0, 1).toLowerCase() + className.substring(1);
		SAXReader sr = new SAXReader();
		Document doc = sr.read(new FileReader("config/ApplicationContext-dao.xml"));
		Element root = doc.getRootElement();
		
		root.addElement("bean")
		.addAttribute("id", lowerClassName+"Dao")
		.addAttribute("class", "cn.tx.dao.impl."+className+"DaoImpl")
		.addElement("property")
		.addAttribute("name", "sessionFactory")
		.addAttribute("ref", "sessionFactory");
		
		XMLWriter writer = new XMLWriter(new FileWriter("config/ApplicationContext-dao.xml"), OutputFormat.createPrettyPrint());
		writer.write(doc);
		writer.close();
		
	}
	
	public static void genServiceConfig(String className) throws Exception{
		String lowerClassName = className.substring(0, 1).toLowerCase() + className.substring(1);
		SAXReader sr = new SAXReader();
		Document doc = sr.read(new FileReader("config/ApplicationContext-service.xml"));
		Element root = doc.getRootElement();
		
		root.addElement("bean")
		.addAttribute("id", lowerClassName+"Service")
		.addAttribute("class", "cn.tx.service.impl."+className+"ServiceImpl")
		.addElement("property")
		.addAttribute("name", lowerClassName+"Dao")
		.addAttribute("ref", lowerClassName+"Dao");
		
		XMLWriter writer = new XMLWriter(new FileWriter("config/ApplicationContext-service.xml"), OutputFormat.createPrettyPrint());
		writer.write(doc);
		writer.close();
		
	}
	public static void genControllerConfig(String className) throws Exception{
		String lowerClassName = className.substring(0, 1).toLowerCase() + className.substring(1);
		SAXReader sr = new SAXReader();
		Document doc = sr.read(new FileReader("config/ApplicationContext-action.xml"));
		Element root = doc.getRootElement();
		
		root.addElement("bean")
		.addAttribute("id", lowerClassName+"Action")
		.addAttribute("class", "cn.tx.controller."+className+"Action")
		.addAttribute("scope", "prototype")
		.addElement("property")
		.addAttribute("name", lowerClassName+"Service")
		.addAttribute("ref", lowerClassName+"Service");
		
		XMLWriter writer = new XMLWriter(new FileWriter("config/ApplicationContext-action.xml"), OutputFormat.createPrettyPrint());
		writer.write(doc);
		writer.close();
		
	}
	public static void genStrutsConfig(String className) throws Exception{
		String lowerClassName = className.substring(0, 1).toLowerCase() + className.substring(1);
		SAXReader sr = new SAXReader();
		//Document doc = sr.read(new FileReader("config/struts.xml"));
		Document doc = sr.read(new File("config/struts.xml"));
		Element root = doc.getRootElement();
		
		Element pele = root.element("package");
		
		Element element = pele.addElement("action")
		.addAttribute("name", lowerClassName+"_*")
		.addAttribute("class", lowerClassName+"Action")
		.addAttribute("method", lowerClassName+"_{1}");
		
		element.addElement("result")
		.addAttribute("name", "success").setText("/WEB-INF/jsps/"+lowerClassName+"/{1}.jsp");
		element.addElement("result")
		.addAttribute("name", "tolist")
		.addAttribute("type", "redirectAction").setText(lowerClassName+"_list");
		
		
		XMLWriter writer = new XMLWriter(new FileWriter("config/struts.xml"), OutputFormat.createPrettyPrint());
		writer.write(doc);
		writer.close();
		
	}
	
}
